import { setStorage, getStorage } from '@/utils/storage'

// 登录结果 - 保存token到vuex中
export default {
  // 命名空间
  namespaced: true,
  state () {
    return {
      // 网页刷新时,要从本地持久化中取出之前登录保存的token。否则用户一刷新就没了token。
      refresh_token: getStorage('refresh_token'),
      token: getStorage('token'),
      // 26.2
      user: {}, // 基本资料
      // 26.5.1编辑--存储
      profile: {} // 用户信息
    }
  },
  mutations: {
    setRefreshToken (state, refreshToken) {
      state.refresh_token = refreshToken
      setStorage('refresh_token', refreshToken)
    },
    setToken (state, token) {
      state.token = token
      setStorage('token', token)
    },
    // 26.2 state里添加user变量: {}, 再定义mutations
    setUser (state, payload) {
      state.user = payload
    },
    // 26.5.1编辑--存储
    setProfile (state, payload) {
      state.profile = payload
    },
    // 26.3个人中心-退出功能
    clearState (state) {
      state.refresh_token = ''
      state.token = ''
      state.user = {}
      state.profile = {}
    }
  }
}
